package com.ye.domain;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.ArrayList;


@AllArgsConstructor
@NoArgsConstructor
@ToString
public class R {

    //    状态码
    private Integer code;
    //    响应数据
    private Object data;
    //    响应备份信息
    private String msg;
    //    成功响应应答数据条数
    private Integer count;

    //    有参构造
    public R(Integer code, String msg) {

        this.code = code;
        this.msg = msg;

    }

    /**
     * 默认成功态返回的条数为0，成功状态码为0
     * @param data
     * @return
     */
    public static R ok(Object data) {

        return new R(0, data, "", 0);

    }

    public static R ok(Object data, String msg) {

        return new R(0, data, msg, 0);

    }

    /**
     * 失败态必须指定失败状态码及失败原因,响应数据条数必为0
     * @param code
     * @param msg
     * @return
     */
    public static R error(Integer code,String msg){

        return new R(code,msg);

    }

//    可手动指定返回状态码或者响应数据条数

    public Integer getCode() {
        return code;
    }

    public R code(Integer code) {
        this.code = code;
        return this;
    }

    public Integer getCount() {
        return count;
    }

    public R count(Integer count) {
        this.count = count;
        return this;
    }

    public Object getData() {
        return data;
    }

    public R data(Object data) {
        this.data = data;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public R msg(String msg) {
        this.msg = msg;
        return this;
    }
}


